import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

//引入element ui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

// 导入全局样式表
import './assets/css/global.css'

import {postRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";
import { initMenu } from './utils/menus';
import {downloadRequest} from './utils/download';


import {getCurrentTime} from "./common/commons";
Vue.prototype.getCurrentTime =getCurrentTime;
import {transformTimestamp} from "./common/commons";
Vue.prototype.transformTimestamp =transformTimestamp;

Vue.config.productionTip = false
//使用ElementUI
Vue.use(ElementUI)

//插件形式使用请求，不需要引入，直接使用this调用
Vue.prototype.postRequest =postRequest;
Vue.prototype.putRequest =putRequest;
Vue.prototype.getRequest =getRequest;
Vue.prototype.deleteRequest =deleteRequest;
Vue.prototype.downloadRequest =downloadRequest;

//路由导航守卫
router.beforeEach((to,from,next) => {
  if (to.path==='/'){
    next()
  }else if (to.path==='/main'){
    if (window.sessionStorage.getItem("tokenStr")){
      initMenu(router,store);
      if (!window.sessionStorage.getItem('user')){
        //判断用户信息是否存在
        return getRequest('/user/info').then(resp=>{
          if (resp){
            //存入用户信息
            window.sessionStorage.setItem('user',JSON.stringify(resp));
            next();
          }
        })
      }
      next();
    }else
    next()
  } else if (to.path==='/enroll'){
    next()
  } else if (to.path==='/allGood'){
    next()
  } else if (to.path==='/allStore'){
    next()
  }else {
    if (window.sessionStorage.getItem("tokenStr")){
      initMenu(router,store);
      if (!window.sessionStorage.getItem('user')){
        //判断用户信息是否存在
        return getRequest('/user/info').then(resp=>{
          if (resp){
            //存入用户信息
            window.sessionStorage.setItem('user',JSON.stringify(resp));
            next();
          }
        })
      }
      next();
    }else {
      if (to.path =='/'){
        next();
      } else {
        next('/?redirect='+to.path);
      }
    }
  }
})


new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
